package qldc.pm.qlktcsdldc.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.qlktcsdldc.NoSuchNguoiDungException;
import qldc.pm.qlktcsdldc.model.NguoiDung;
import qldc.pm.qlktcsdldc.model.impl.NguoiDungImpl;
import qldc.pm.qlktcsdldc.model.impl.NguoiDungModelImpl;
import qldc.pm.qlktcsdldc.service.persistence.NguoiDungPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the nguoi dung service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see NguoiDungPersistence
 * @see NguoiDungUtil
 * @generated
 */
public class NguoiDungPersistenceImpl extends BasePersistenceImpl<NguoiDung>
    implements NguoiDungPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link NguoiDungUtil} to access the nguoi dung persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = NguoiDungImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungModelImpl.FINDER_CACHE_ENABLED, NguoiDungImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungModelImpl.FINDER_CACHE_ENABLED, NguoiDungImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG =
        new FinderPath(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungModelImpl.FINDER_CACHE_ENABLED, NguoiDungImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByIDQLKT_NguoiDung",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG =
        new FinderPath(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungModelImpl.FINDER_CACHE_ENABLED, NguoiDungImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByIDQLKT_NguoiDung", new String[] { Long.class.getName() },
            NguoiDungModelImpl.IDQLKT_NGUOIDUNG_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_IDQLKT_NGUOIDUNG = new FinderPath(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByIDQLKT_NguoiDung", new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_IDQLKT_NGUOIDUNG_IDQLKT_NGUOIDUNG_2 =
        "nguoiDung.IDQLKT_NguoiDung = ?";
    private static final String _SQL_SELECT_NGUOIDUNG = "SELECT nguoiDung FROM NguoiDung nguoiDung";
    private static final String _SQL_SELECT_NGUOIDUNG_WHERE = "SELECT nguoiDung FROM NguoiDung nguoiDung WHERE ";
    private static final String _SQL_COUNT_NGUOIDUNG = "SELECT COUNT(nguoiDung) FROM NguoiDung nguoiDung";
    private static final String _SQL_COUNT_NGUOIDUNG_WHERE = "SELECT COUNT(nguoiDung) FROM NguoiDung nguoiDung WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "nguoiDung.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No NguoiDung exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No NguoiDung exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(NguoiDungPersistenceImpl.class);
    private static NguoiDung _nullNguoiDung = new NguoiDungImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<NguoiDung> toCacheModel() {
                return _nullNguoiDungCacheModel;
            }
        };

    private static CacheModel<NguoiDung> _nullNguoiDungCacheModel = new CacheModel<NguoiDung>() {
            @Override
            public NguoiDung toEntityModel() {
                return _nullNguoiDung;
            }
        };

    public NguoiDungPersistenceImpl() {
        setModelClass(NguoiDung.class);
    }

    /**
     * Returns all the nguoi dungs where IDQLKT_NguoiDung = &#63;.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @return the matching nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<NguoiDung> findByIDQLKT_NguoiDung(long IDQLKT_NguoiDung)
        throws SystemException {
        return findByIDQLKT_NguoiDung(IDQLKT_NguoiDung, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the nguoi dungs where IDQLKT_NguoiDung = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.NguoiDungModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @param start the lower bound of the range of nguoi dungs
     * @param end the upper bound of the range of nguoi dungs (not inclusive)
     * @return the range of matching nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<NguoiDung> findByIDQLKT_NguoiDung(long IDQLKT_NguoiDung,
        int start, int end) throws SystemException {
        return findByIDQLKT_NguoiDung(IDQLKT_NguoiDung, start, end, null);
    }

    /**
     * Returns an ordered range of all the nguoi dungs where IDQLKT_NguoiDung = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.NguoiDungModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @param start the lower bound of the range of nguoi dungs
     * @param end the upper bound of the range of nguoi dungs (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<NguoiDung> findByIDQLKT_NguoiDung(long IDQLKT_NguoiDung,
        int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG;
            finderArgs = new Object[] { IDQLKT_NguoiDung };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG;
            finderArgs = new Object[] {
                    IDQLKT_NguoiDung,
                    
                    start, end, orderByComparator
                };
        }

        List<NguoiDung> list = (List<NguoiDung>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (NguoiDung nguoiDung : list) {
                if ((IDQLKT_NguoiDung != nguoiDung.getIDQLKT_NguoiDung())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_NGUOIDUNG_WHERE);

            query.append(_FINDER_COLUMN_IDQLKT_NGUOIDUNG_IDQLKT_NGUOIDUNG_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(NguoiDungModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(IDQLKT_NguoiDung);

                if (!pagination) {
                    list = (List<NguoiDung>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<NguoiDung>(list);
                } else {
                    list = (List<NguoiDung>) QueryUtil.list(q, getDialect(),
                            start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first nguoi dung in the ordered set where IDQLKT_NguoiDung = &#63;.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching nguoi dung
     * @throws qldc.pm.qlktcsdldc.NoSuchNguoiDungException if a matching nguoi dung could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung findByIDQLKT_NguoiDung_First(long IDQLKT_NguoiDung,
        OrderByComparator orderByComparator)
        throws NoSuchNguoiDungException, SystemException {
        NguoiDung nguoiDung = fetchByIDQLKT_NguoiDung_First(IDQLKT_NguoiDung,
                orderByComparator);

        if (nguoiDung != null) {
            return nguoiDung;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("IDQLKT_NguoiDung=");
        msg.append(IDQLKT_NguoiDung);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchNguoiDungException(msg.toString());
    }

    /**
     * Returns the first nguoi dung in the ordered set where IDQLKT_NguoiDung = &#63;.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching nguoi dung, or <code>null</code> if a matching nguoi dung could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung fetchByIDQLKT_NguoiDung_First(long IDQLKT_NguoiDung,
        OrderByComparator orderByComparator) throws SystemException {
        List<NguoiDung> list = findByIDQLKT_NguoiDung(IDQLKT_NguoiDung, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last nguoi dung in the ordered set where IDQLKT_NguoiDung = &#63;.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching nguoi dung
     * @throws qldc.pm.qlktcsdldc.NoSuchNguoiDungException if a matching nguoi dung could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung findByIDQLKT_NguoiDung_Last(long IDQLKT_NguoiDung,
        OrderByComparator orderByComparator)
        throws NoSuchNguoiDungException, SystemException {
        NguoiDung nguoiDung = fetchByIDQLKT_NguoiDung_Last(IDQLKT_NguoiDung,
                orderByComparator);

        if (nguoiDung != null) {
            return nguoiDung;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("IDQLKT_NguoiDung=");
        msg.append(IDQLKT_NguoiDung);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchNguoiDungException(msg.toString());
    }

    /**
     * Returns the last nguoi dung in the ordered set where IDQLKT_NguoiDung = &#63;.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching nguoi dung, or <code>null</code> if a matching nguoi dung could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung fetchByIDQLKT_NguoiDung_Last(long IDQLKT_NguoiDung,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByIDQLKT_NguoiDung(IDQLKT_NguoiDung);

        if (count == 0) {
            return null;
        }

        List<NguoiDung> list = findByIDQLKT_NguoiDung(IDQLKT_NguoiDung,
                count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Removes all the nguoi dungs where IDQLKT_NguoiDung = &#63; from the database.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByIDQLKT_NguoiDung(long IDQLKT_NguoiDung)
        throws SystemException {
        for (NguoiDung nguoiDung : findByIDQLKT_NguoiDung(IDQLKT_NguoiDung,
                QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(nguoiDung);
        }
    }

    /**
     * Returns the number of nguoi dungs where IDQLKT_NguoiDung = &#63;.
     *
     * @param IDQLKT_NguoiDung the i d q l k t_ nguoi dung
     * @return the number of matching nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByIDQLKT_NguoiDung(long IDQLKT_NguoiDung)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_IDQLKT_NGUOIDUNG;

        Object[] finderArgs = new Object[] { IDQLKT_NguoiDung };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_NGUOIDUNG_WHERE);

            query.append(_FINDER_COLUMN_IDQLKT_NGUOIDUNG_IDQLKT_NGUOIDUNG_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(IDQLKT_NguoiDung);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the nguoi dung in the entity cache if it is enabled.
     *
     * @param nguoiDung the nguoi dung
     */
    @Override
    public void cacheResult(NguoiDung nguoiDung) {
        EntityCacheUtil.putResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungImpl.class, nguoiDung.getPrimaryKey(), nguoiDung);

        nguoiDung.resetOriginalValues();
    }

    /**
     * Caches the nguoi dungs in the entity cache if it is enabled.
     *
     * @param nguoiDungs the nguoi dungs
     */
    @Override
    public void cacheResult(List<NguoiDung> nguoiDungs) {
        for (NguoiDung nguoiDung : nguoiDungs) {
            if (EntityCacheUtil.getResult(
                        NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
                        NguoiDungImpl.class, nguoiDung.getPrimaryKey()) == null) {
                cacheResult(nguoiDung);
            } else {
                nguoiDung.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all nguoi dungs.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(NguoiDungImpl.class.getName());
        }

        EntityCacheUtil.clearCache(NguoiDungImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the nguoi dung.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(NguoiDung nguoiDung) {
        EntityCacheUtil.removeResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungImpl.class, nguoiDung.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @Override
    public void clearCache(List<NguoiDung> nguoiDungs) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (NguoiDung nguoiDung : nguoiDungs) {
            EntityCacheUtil.removeResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
                NguoiDungImpl.class, nguoiDung.getPrimaryKey());
        }
    }

    /**
     * Creates a new nguoi dung with the primary key. Does not add the nguoi dung to the database.
     *
     * @param IDQLKT_NguoiDung the primary key for the new nguoi dung
     * @return the new nguoi dung
     */
    @Override
    public NguoiDung create(long IDQLKT_NguoiDung) {
        NguoiDung nguoiDung = new NguoiDungImpl();

        nguoiDung.setNew(true);
        nguoiDung.setPrimaryKey(IDQLKT_NguoiDung);

        return nguoiDung;
    }

    /**
     * Removes the nguoi dung with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param IDQLKT_NguoiDung the primary key of the nguoi dung
     * @return the nguoi dung that was removed
     * @throws qldc.pm.qlktcsdldc.NoSuchNguoiDungException if a nguoi dung with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung remove(long IDQLKT_NguoiDung)
        throws NoSuchNguoiDungException, SystemException {
        return remove((Serializable) IDQLKT_NguoiDung);
    }

    /**
     * Removes the nguoi dung with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the nguoi dung
     * @return the nguoi dung that was removed
     * @throws qldc.pm.qlktcsdldc.NoSuchNguoiDungException if a nguoi dung with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung remove(Serializable primaryKey)
        throws NoSuchNguoiDungException, SystemException {
        Session session = null;

        try {
            session = openSession();

            NguoiDung nguoiDung = (NguoiDung) session.get(NguoiDungImpl.class,
                    primaryKey);

            if (nguoiDung == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchNguoiDungException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(nguoiDung);
        } catch (NoSuchNguoiDungException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected NguoiDung removeImpl(NguoiDung nguoiDung)
        throws SystemException {
        nguoiDung = toUnwrappedModel(nguoiDung);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(nguoiDung)) {
                nguoiDung = (NguoiDung) session.get(NguoiDungImpl.class,
                        nguoiDung.getPrimaryKeyObj());
            }

            if (nguoiDung != null) {
                session.delete(nguoiDung);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (nguoiDung != null) {
            clearCache(nguoiDung);
        }

        return nguoiDung;
    }

    @Override
    public NguoiDung updateImpl(qldc.pm.qlktcsdldc.model.NguoiDung nguoiDung)
        throws SystemException {
        nguoiDung = toUnwrappedModel(nguoiDung);

        boolean isNew = nguoiDung.isNew();

        NguoiDungModelImpl nguoiDungModelImpl = (NguoiDungModelImpl) nguoiDung;

        Session session = null;

        try {
            session = openSession();

            if (nguoiDung.isNew()) {
                session.save(nguoiDung);

                nguoiDung.setNew(false);
            } else {
                session.merge(nguoiDung);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !NguoiDungModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((nguoiDungModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        nguoiDungModelImpl.getOriginalIDQLKT_NguoiDung()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_IDQLKT_NGUOIDUNG,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG,
                    args);

                args = new Object[] { nguoiDungModelImpl.getIDQLKT_NguoiDung() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_IDQLKT_NGUOIDUNG,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_NGUOIDUNG,
                    args);
            }
        }

        EntityCacheUtil.putResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
            NguoiDungImpl.class, nguoiDung.getPrimaryKey(), nguoiDung);

        return nguoiDung;
    }

    protected NguoiDung toUnwrappedModel(NguoiDung nguoiDung) {
        if (nguoiDung instanceof NguoiDungImpl) {
            return nguoiDung;
        }

        NguoiDungImpl nguoiDungImpl = new NguoiDungImpl();

        nguoiDungImpl.setNew(nguoiDung.isNew());
        nguoiDungImpl.setPrimaryKey(nguoiDung.getPrimaryKey());

        nguoiDungImpl.setIDQLKT_NguoiDung(nguoiDung.getIDQLKT_NguoiDung());
        nguoiDungImpl.setGroupId(nguoiDung.getGroupId());
        nguoiDungImpl.setCompanyId(nguoiDung.getCompanyId());
        nguoiDungImpl.setUserId(nguoiDung.getUserId());
        nguoiDungImpl.setUserName(nguoiDung.getUserName());
        nguoiDungImpl.setCreateDate(nguoiDung.getCreateDate());
        nguoiDungImpl.setModifiedDate(nguoiDung.getModifiedDate());
        nguoiDungImpl.setTenDangNhap(nguoiDung.getTenDangNhap());
        nguoiDungImpl.setHoVaTen(nguoiDung.getHoVaTen());
        nguoiDungImpl.setSoCMND(nguoiDung.getSoCMND());
        nguoiDungImpl.setIDTinhThuongTru(nguoiDung.getIDTinhThuongTru());
        nguoiDungImpl.setIDQuanHuyenThuongTru(nguoiDung.getIDQuanHuyenThuongTru());
        nguoiDungImpl.setIDPhuongXaThuongTru(nguoiDung.getIDPhuongXaThuongTru());
        nguoiDungImpl.setDiaChiThuongTru(nguoiDung.getDiaChiThuongTru());
        nguoiDungImpl.setIDQLKT_DanhMucSoBanNganh(nguoiDung.getIDQLKT_DanhMucSoBanNganh());
        nguoiDungImpl.setGioiTinh(nguoiDung.getGioiTinh());
        nguoiDungImpl.setDanToc(nguoiDung.getDanToc());
        nguoiDungImpl.setChuThich(nguoiDung.getChuThich());
        nguoiDungImpl.setDeleted(nguoiDung.getDeleted());
        nguoiDungImpl.setKichHoat(nguoiDung.getKichHoat());

        return nguoiDungImpl;
    }

    /**
     * Returns the nguoi dung with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the nguoi dung
     * @return the nguoi dung
     * @throws qldc.pm.qlktcsdldc.NoSuchNguoiDungException if a nguoi dung with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung findByPrimaryKey(Serializable primaryKey)
        throws NoSuchNguoiDungException, SystemException {
        NguoiDung nguoiDung = fetchByPrimaryKey(primaryKey);

        if (nguoiDung == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchNguoiDungException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return nguoiDung;
    }

    /**
     * Returns the nguoi dung with the primary key or throws a {@link qldc.pm.qlktcsdldc.NoSuchNguoiDungException} if it could not be found.
     *
     * @param IDQLKT_NguoiDung the primary key of the nguoi dung
     * @return the nguoi dung
     * @throws qldc.pm.qlktcsdldc.NoSuchNguoiDungException if a nguoi dung with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung findByPrimaryKey(long IDQLKT_NguoiDung)
        throws NoSuchNguoiDungException, SystemException {
        return findByPrimaryKey((Serializable) IDQLKT_NguoiDung);
    }

    /**
     * Returns the nguoi dung with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the nguoi dung
     * @return the nguoi dung, or <code>null</code> if a nguoi dung with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        NguoiDung nguoiDung = (NguoiDung) EntityCacheUtil.getResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
                NguoiDungImpl.class, primaryKey);

        if (nguoiDung == _nullNguoiDung) {
            return null;
        }

        if (nguoiDung == null) {
            Session session = null;

            try {
                session = openSession();

                nguoiDung = (NguoiDung) session.get(NguoiDungImpl.class,
                        primaryKey);

                if (nguoiDung != null) {
                    cacheResult(nguoiDung);
                } else {
                    EntityCacheUtil.putResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
                        NguoiDungImpl.class, primaryKey, _nullNguoiDung);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(NguoiDungModelImpl.ENTITY_CACHE_ENABLED,
                    NguoiDungImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return nguoiDung;
    }

    /**
     * Returns the nguoi dung with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param IDQLKT_NguoiDung the primary key of the nguoi dung
     * @return the nguoi dung, or <code>null</code> if a nguoi dung with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public NguoiDung fetchByPrimaryKey(long IDQLKT_NguoiDung)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) IDQLKT_NguoiDung);
    }

    /**
     * Returns all the nguoi dungs.
     *
     * @return the nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<NguoiDung> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the nguoi dungs.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.NguoiDungModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of nguoi dungs
     * @param end the upper bound of the range of nguoi dungs (not inclusive)
     * @return the range of nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<NguoiDung> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the nguoi dungs.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.NguoiDungModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of nguoi dungs
     * @param end the upper bound of the range of nguoi dungs (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<NguoiDung> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<NguoiDung> list = (List<NguoiDung>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_NGUOIDUNG);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_NGUOIDUNG;

                if (pagination) {
                    sql = sql.concat(NguoiDungModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<NguoiDung>) QueryUtil.list(q, getDialect(),
                            start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<NguoiDung>(list);
                } else {
                    list = (List<NguoiDung>) QueryUtil.list(q, getDialect(),
                            start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the nguoi dungs from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (NguoiDung nguoiDung : findAll()) {
            remove(nguoiDung);
        }
    }

    /**
     * Returns the number of nguoi dungs.
     *
     * @return the number of nguoi dungs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_NGUOIDUNG);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the nguoi dung persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.qlktcsdldc.model.NguoiDung")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<NguoiDung>> listenersList = new ArrayList<ModelListener<NguoiDung>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<NguoiDung>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(NguoiDungImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
